<?php
/**
 * > 装備データ
 *
 * LICENSE: Miracle Maple by Mirage is licensed
 * under a Creative Commons 表示-非営利-継承 2.1 日本 License.
 *
 * @category   equip
 * @package    maple-story.jp
 * @version    8.0.0
 * @link       http://maple-story.jp/equipment/
 * @since      File available since Release 8.0.0
 * @copyright  Copyright 2011 Miracle Maple
 * @license    http://creativecommons.org/licenses/by-nc-sa/2.1/jp/     Creative Commons BY-NC-SA
 */
class EquipmentController extends Zend_Controller_Action
{
	protected $_equipment;
	protected $_equipmentGroup;
	protected $_equipmentDirectory;
	
	public function init()
	{
		$this->_equipment = new Model_TrnEquipment();
		$equipmentGroup = new Model_MstEquipmentGroup();
		$this->_equipmentGroup = $equipmentGroup->fetchAllArray('wid');
		$this->_equipmentDirectory = $equipmentGroup->fetchAllArray('wid', 'directory');

		$this->view->headLink()->appendStylesheet('/css/equipment.css');
		$this->view->headTitle('装備', 'PREPEND');
	}
	
    /**
     * トップ
     */
    public function indexAction()
    {
		$this->view->equipmentGroup = $this->_equipmentGroup;
		
		// パンくず
        $topicPath = array();
        $topicPath[] = array('name' => '装備');
		$this->view->topicPath = $topicPath;
    }
    
    public function searchAction()
    {
		$rowsetEquipment = array();
		$groupCd = $this->_getParam('group');
		if (!empty($groupCd)) {
			$rowsetEquipment = $this->_equipment->search(
				array('groupCd' => $groupCd)
			);
		}
		
		foreach ($rowsetEquipment as &$rowEquipment) {
			if ($rowEquipment->req_job_cd > 0) {
				// 1 : 戦 / 2 : 魔 / 4 : 弓 / 8 : 盗 / 16 : 海
				$requireJobCd = array_reverse(str_split(substr('00000' . decbin($rowEquipment->req_job_cd), -5)));
				$requireJob = '<span class="invalid">初</span>';
				$requireJob .= $requireJobCd[0] ? '戦' : '<span class="invalid">戦</span>';
				$requireJob .= $requireJobCd[1] ? '魔' : '<span class="invalid">魔</span>';
				$requireJob .= $requireJobCd[2] ? '弓' : '<span class="invalid">弓</span>';
				$requireJob .= $requireJobCd[3] ? '盗' : '<span class="invalid">盗</span>';
				$requireJob .= $requireJobCd[4] ? '海' : '<span class="invalid">海</span>';
			} else if ($rowEquipment->req_job_cd == -1) {
				$requireJob = '初<span class="invalid">戦魔弓盗海</span>';
			} else if ($rowEquipment->req_job_cd == 0) {
				$requireJob = '全職業';
			}
			$rowEquipment->req_job = $requireJob;
		}
		
		$this->view->equips = $rowsetEquipment;
		$this->view->directory = $this->_equipmentDirectory;
		
		$groupCd = explode(',', $groupCd);
		$groupCd = $groupCd[0];

		// パンくず
        $topicPath = array();
        $topicPath[] = array('name' => '装備', 'url' => '/equipment/');
        $topicPath[] = array('name' => $this->_equipmentGroup[$groupCd]);
		$this->view->topicPath = $topicPath;
		$this->view->headTitle($this->_equipmentGroup[$groupCd], 'PREPEND');
    }
    
    public function dataAction()
    {
        $rowsEquip = $this->_equipment->getDataByName($this->_getParam('name'));
		
		if ($this->_getParam('index') != 0) {
			$index = $this->_getParam('index') - 1;
		} else {
			$index = 0;
		}
		
		if ($index > count($rowsEquip)) {
			throw new Zend_Controller_Action_Exception('Index number which has provided was out of range.');
		} else if (!$rowsEquip[$index] instanceof Model_TrnEquip) {
			throw new Zend_Controller_Action_Exception('Invalid Data Model has provided in equip detail.');
		}
		
		$rowEquip = $rowsEquip[$index];
		
		$this->view->equip = $rowEquip;
		$this->view->headTitle('装備', 'PREPEND');
		$this->view->headTitle($rowEquip->name, 'PREPEND');
		
		// パンくず設定
        $topicPath = array();
        $topicPath[] = array('name' => '装備', 'url' => '/装備/');
        $topicPath[] = array('name' => $rowEquip->name);
		$this->view->topicPath = $topicPath;
	
		if (count($rowsEquip) == 1) {
			$this->view->headMeta()->appendName('canonical', '/装備/' . $rowEquip->name . '/');
		} else {
			$this->view->headMeta()->appendName('canonical', '/装備/' . $rowEquip->name . '/' . $index . '/');
		}
	}
	
	public function search2Action()
	{
		$rowsEquip = $this->_equip->search($this->getRequest()->getParams());

		if (count($rowsEquip) == 1) {
			return $this->_forward('detail', 'equip', 'default', array('name' => $rowsEquip[0]->name));
		}
			
		$this->view->equips = $rowsEquip;
		$this->view->searchOptions = $this->getRequest()->getParams();
	}
        
}
